<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订阅人姓名" prop="custumerName" label-width="100px">
        <el-input
          v-model="queryParams.custumerName"
          placeholder="请输入订阅人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订阅人手机号码" prop="mobile" label-width="110px">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入订阅人手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="statue">
        <el-select v-model="queryParams.statue" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.magazine_order_statue"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订阅时间">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['parent:course:export']"
              >导出</el-button>
            </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column label="订阅人手机号码" align="center" prop="mobile"/>
      <el-table-column label="订单号" align="center" prop="orderNo"/>
      <el-table-column label="订阅人姓名" align="center" prop="custumerName"/>
      <el-table-column label="状态" align="center" prop="statue">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.magazine_order_statue" :value="scope.row.statue"/>
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="totalPrice"/>
      <el-table-column label="套数" align="center" prop="num"/>
<!--      <el-table-column label="收货地址" align="center" prop="address"/>-->
      <el-table-column label="所在地市" align="center" prop="city"/>
      <el-table-column label="收件人姓名" align="center" prop="receiver"/>
      <el-table-column label="付款时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订阅时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单快递号" align="center" prop="trackingNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['magazine:order:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['magazine:order:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime">
          <el-input v-model="form.payTime" placeholder="付款时间"/>
        </el-form-item>
        <el-form-item label="快递订单号" prop="trackingNum">
          <el-input v-model="form.trackingNum" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="订阅人姓名" prop="custumerName">
          <el-input v-model="form.custumerName" placeholder="请输入订阅人姓名"/>
        </el-form-item>
        <el-form-item label="订阅人手机号码" prop="mobile" label-width="120px">
          <el-input v-model="form.mobile" placeholder="请输入订阅人手机号码"/>
        </el-form-item>
        <el-form-item label="订阅人联系电话" prop="phone" label-width="120px">
          <el-input v-model="form.phone" placeholder="请输入订阅人联系电话"/>
        </el-form-item>
        <el-form-item label="订阅人电子邮箱" prop="email" label-width="120px">
          <el-input v-model="form.email" placeholder="请输入订阅人电子邮箱"/>
        </el-form-item>
        <el-form-item label="所属公司名称" prop="company">
          <el-input v-model="form.company" placeholder="请输入所属公司名称"/>
        </el-form-item>
        <el-form-item label="收货地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="选择城市" prop="city">
          <el-select v-model="form.city" placeholder="选择城市(仅限河南省内)" clearable
                     :style="{width: '100%'}"
          >
            <el-option v-for="(item, index) in cityList" :key="index" :label="item.CITY"
                       :value="item.CITY"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮政编码" prop="postcode">
          <el-input v-model="form.postcode" placeholder="请输入邮政编码"/>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收件人姓名"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价"/>
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceTitle">
          <el-input v-model="form.invoiceTitle" placeholder="请输入发票抬头"/>
        </el-form-item>
        <el-form-item label="税号" prop="taxNo">
          <el-input v-model="form.taxNo" placeholder="请输入税号"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from '@/api/magazine/order'
import cityList from '@/assets/city.json'

export default {
  name: 'Order',
  dicts: ['magazine_order_statue'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      cityList: cityList.RECORDS,
      daterangeCreateDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custumerName: null,
        mobile: null,
        statue: null,
        params: {
          beginCreateDate: null,
          endCreateDate:null

        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        custumerName: [
          { required: true, message: '订阅人姓名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '订阅人电子邮箱不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '订阅人手机号码不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '收货地址不能为空', trigger: 'blur' }
        ],
        receiver: [
          { required: true, message: '收件人姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        trackingNum: null,
        custumerName: null,
        phone: null,
        email: null,
        mobile: null,
        company: null,
        address: null,
        city: null,
        postcode: null,
        receiver: null,
        statue: null,
        totalPrice: null,
        invoiceTitle: null,
        taxNo: null,
        payTime: null,
        payType: null,
        createDate: null,
        updateDate: null,
        remarks: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
        this.queryParams.params['beginCreateDate'] = this.daterangeCreateDate[0]
        this.queryParams.params['endCreateDate'] = this.daterangeCreateDate[1]
      }
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加订单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改订单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('magazine/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
